<?php

namespace app\models;

use think\Model;

/**
 * Class TimetableModel
 * @package app\models
 */
class TimetableModel extends Model
{
    protected $table = 'edu_class_manage_details_lists';

    /**
     * @param $param
     *
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public function getTimetableLists($param,$room_id)
    {
        $GradesDetailsModel = new GradesDetailsModel();  // 班级
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        $now_day = date("Y-m-d");
        $now_time=strtotime(date("H:i:s"));
        $where['class_date'] = $now_day;
        $where['class_room_id'] = $room_id;
		if(get_current_type() == 4 || get_current_type() == 5){
			$where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['campus_id'] = ['in',get_current_campus_id()];
		}

        //获取教室课表
        if(get_current_type() == 5){
            if(get_current_group_type() == 2){  //老师类
                $grades_res = $GradesDetailsModel->where(['user_teacher_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where['class_manage_id'] = ['in',$grades_res_str];
            }else if(get_current_group_type() == 3){  //助教类
                $grades_res = $GradesDetailsModel->where(['user_assistant_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where['class_manage_id'] = ['in',$grades_res_str];
            }else if(get_current_group_type() == 4){ //班主任
                $grades_res = $GradesDetailsModel->where(['user_master_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where['class_manage_id'] = ['in',$grades_res_str];
            }else if(get_current_group_type() == 5){ //教务类
                $grades_res = $GradesDetailsModel->where(['user_edu_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where['class_manage_id'] = ['in',$grades_res_str];
            }
        }


        $listData = self::where($where)
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        $studentsubjectinfosclassModel = new StudentSubjectInfosClassModel();
        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {

                $item['class_time'] = $item['class_date'].' '.$item['class_subject_start'].'-'.$item['class_subject_end'];
                $start = strtotime($item['class_subject_start']);  //开始时间
                $end = strtotime($item['class_subject_end']);  //结束时间
                $item['duration'] = ceil(($end - $start)/60).'分钟';
                $nums = $studentsubjectinfosclassModel->where(['class_manage_details_id' => $item['id']])->select();
                if($item['status'] == 1){
                    $already_nums = 0;
                    foreach ($nums as $key => $value_){
                        if($value_['attendance'] == 1){
                            $already_nums ++;
                        }
                    }
                    $item['actual_number'] = $already_nums.'/'.count($nums);

                }else if($item['status'] == 0){
                    $item['actual_number'] = '0/'.count($nums);
                }

                if($now_time > $end && $item['status'] == 0){  //当前时间大于结束时间
                    $item['status_text'] = $item['status'] ? '<span style="color:#01AAED">已签到</span>' : ' <span  style="color:#FF5722">【已过期】未签到</span>';
                }else if($now_time > $start && $now_time < $end && $item['status'] == 0){
                    $item['status_text'] = $item['status'] ? '<span style="color:#01AAED">已签到</span>' : ' <span  style="color:#FF5722">【已超时】未签到</span>';
                }else{
                    $item['status_text'] = $item['status'] ? '<span style="color:#01AAED">已签到</span>' : ' <span  style="color:#FF5722">未签到</span>';
                }


            }
        }
        return $listData;
    }
}
